package com.ccepc.algmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccepc.algmanage.mapper.PushRuleMapper;
import com.ccepc.algmanage.service.PushRuleService;
import com.ccepc.algmanage.utils.AuthInfoUtils;
import com.ccepc.algmanage.utils.CommonUtils;
import com.ccepc.common.core.util.R;
import com.ccepc.demo.api.entity.PushRule;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;

/**
 *
 *
 * @author pigx code generator
 * @date 2023-11-17 17:38:29
 */
@Service
public class PushRuleServiceImpl extends ServiceImpl<PushRuleMapper, PushRule> implements PushRuleService {
    @Transactional
    @Override
    public R saveOrUpdateP(PushRule pushRule) {
        pushRule.setOnlyOnce(pushRule.getTimeUnit() == null ? 1 : 0);
        int saveOrUpdate = CommonUtils.saveOrUpdate(pushRule, this);
        if (saveOrUpdate == -1) {
            return R.failed("修改失败");
        }
        if (pushRule.getOnlyOnce() == 1 && saveOrUpdate == 1) {
            this.update(
                    new LambdaUpdateWrapper<PushRule>()
                            .eq(PushRule::getId, pushRule.getId())
                            .set(PushRule::getPushInterval, null)
                            .set(PushRule::getTimeUnit, null)
            );
        }
        return R.ok();
    }
}
